import request from '../../../components/Request';

/**
 * 根据上级机构Id查询组织机构列表
 * @param parentId 上级机构id
 * @returns {Promise<*>}
 */
export async function listDeptByParentId(parentId) {
  let result = await request.post({ url: './dept/listDeptByParentId.do', data: { parentId: parentId } });
  let deptList = result.data.map(function(item) {
    if (!item.isLeaf) item.children = [];
    return item;
  });
  return deptList;
}

/**
 * 查询机构列表
 * @returns {Promise<*>}
 */
export async function listDept(orgId,name) {
  let result = await request.post({ url: './dept/listDept.do', data: {orgId: orgId, name: name } });
  let deptList = result.data.map(function(item) {
    if (!item.isLeaf) item.children = [];
    return item;
  });
  return deptList;
}

/**
 * 查询机构类型字典
 * @returns {Promise<*>}
 */
export async function listDeptType() {
  let result = await request.post({ url: './dict/getDict.do', data: { key: 'deptType' } });
  return result.data;
}

/**
 * 保存机构信息
 * @param dept 机构信息
 * @returns {Promise<*>}
 */
export async function saveDept(dept) {
  let result = await request.post({ url: './dept/saveDept.do', data: dept });
  return result.data;
}

/**
 * 删除机构
 * @param dept 机构
 * @returns {Promise<void>}
 */
export async function deleteDept(dept) {
  let pDept = { id: dept.id, cascade: dept.cascade, parentId: dept.parentId };
  return await request.post({ url: './dept/deleteDept.do', data: pDept });
}
